// pages/exhibition/check-in/check-in.js
Page({
  data: {
    showManualModal: false,
    showSuccessModal: false,
    manualCode: '',
    checkinResult: {},
    myReservations: [
      {
        id: 1,
        sessionId: 1,
        sessionTitle: '生命教育主题展',
        sessionDate: '2025.08.20',
        sessionTime: '09:00-11:00',
        location: '生命教育展馆A厅',
        reservationCode: 'EX20250820001',
        status: 'confirmed',
        statusText: '已确认'
      },
      {
        id: 2,
        sessionId: 3,
        sessionTitle: '医学知识科普展',
        sessionDate: '2025.08.25',
        sessionTime: '10:00-12:00',
        location: '生命教育展馆C厅',
        reservationCode: 'EX20250825002',
        status: 'checked_in',
        statusText: '已签到'
      }
    ]
  },

  onLoad(options) {
    console.log('签到页面加载');
    this.loadReservations();
  },

  onShow() {
    // 页面显示时刷新数据
    this.loadReservations();
  },

  // 加载预约数据
  loadReservations() {
    // 这里可以从服务器获取用户预约数据
    console.log('加载预约数据');
  },

  // 扫码签到
  scanCodeCheckIn() {
    wx.scanCode({
      success: (res) => {
        console.log('扫码结果:', res);
        this.processCheckIn(res.result);
      },
      fail: (err) => {
        console.log('扫码失败:', err);
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  },

  // 显示手动输入弹窗
  showManualCheckIn() {
    this.setData({
      showManualModal: true,
      manualCode: ''
    });
  },

  // 隐藏手动输入弹窗
  hideManualCheckIn() {
    this.setData({
      showManualModal: false
    });
  },

  // 手动输入预约码
  onManualCodeInput(e) {
    this.setData({
      manualCode: e.detail.value
    });
  },

  // 提交手动签到
  submitManualCheckIn() {
    const { manualCode } = this.data;
    
    if (!manualCode.trim()) {
      wx.showToast({
        title: '请输入预约码',
        icon: 'none'
      });
      return;
    }

    this.processCheckIn(manualCode);
    this.hideManualCheckIn();
  },

  // 快速签到
  quickCheckIn(e) {
    const id = e.currentTarget.dataset.id;
    const reservation = this.data.myReservations.find(item => item.id == id);
    
    if (!reservation) {
      wx.showToast({
        title: '预约信息不存在',
        icon: 'none'
      });
      return;
    }

    if (reservation.status !== 'confirmed') {
      wx.showToast({
        title: '该预约无法签到',
        icon: 'none'
      });
      return;
    }

    this.processCheckIn(reservation.reservationCode);
  },

  // 处理签到
  processCheckIn(code) {
    wx.showLoading({
      title: '签到中...'
    });

    // 模拟签到处理
    setTimeout(() => {
      wx.hideLoading();
      
      // 查找匹配的预约
      const reservation = this.data.myReservations.find(item => item.reservationCode === code);
      
      if (reservation && reservation.status === 'confirmed') {
        // 更新预约状态
        const updatedReservations = this.data.myReservations.map(item => {
          if (item.id === reservation.id) {
            return {
              ...item,
              status: 'checked_in',
              statusText: '已签到'
            };
          }
          return item;
        });

        this.setData({
          myReservations: updatedReservations,
          checkinResult: {
            sessionTitle: reservation.sessionTitle,
            sessionDate: reservation.sessionDate,
            sessionTime: reservation.sessionTime,
            location: reservation.location
          },
          showSuccessModal: true
        });

        wx.showToast({
          title: '签到成功',
          icon: 'success'
        });
      } else {
        wx.showToast({
          title: '预约码无效或已签到',
          icon: 'none'
        });
      }
    }, 1500);
  },

  // 隐藏成功弹窗
  hideSuccessModal() {
    this.setData({
      showSuccessModal: false
    });
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: '展馆签到',
      path: '/pages/exhibition/check-in/check-in'
    };
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadReservations();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
